﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using Wicresoft.Framework.Organization;
using Wicresoft.Busi.BizLogic;
using Wicresoft.Busi.Entity;

public partial class NewMeetingRoom_RoomInfoDetail : BaseAuthenticationPage
{
    #region Property RoomID
    public int RoomID
    {
        get
        {
            if (Request["RoomID"] != null)
                return int.Parse(Request["RoomID"].ToString());
            else
                return int.MinValue;
        }

    }
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        this.UseTabber = true;
        if (!IsPostBack)
        {
            initRoomType();
            initRoomStatus();
            this.tdSave.Visible = true;

            if (RoomID != int.MinValue)
            {
                LoadRoomInfo();
                this.fg1.Visible = false;
                this.tdReset.Visible = false;
                this.fg2.Visible = true;
                this.tdDelete.Visible = true;
            }
            else
            {
                imgRoom.ImageUrl = "../Images/NoPreview.jpg";
                this.fg1.Visible = true;
                this.tdReset.Visible = true;
                this.fg2.Visible = false;
                this.tdDelete.Visible = false;
            }
        }
    }

    #region 初始化会议室类型下拉框
    private void initRoomType()
    {
        DataTable dt = BLDic_MeetingRoom.GetRoomType();

        this.ddlRoomType.DataSource = dt;
        this.ddlRoomType.DataTextField = Dic_MeetingRoom.SQLCOL_PROPERTYNAME;
        this.ddlRoomType.DataValueField = Dic_MeetingRoom.SQLCOL_PROPERTYID;
        this.ddlRoomType.DataBind();

    }
    #endregion

    #region 初始化会议室使用状态下拉框
    private void initRoomStatus()
    {
        DataTable dt = BLDic_MeetingRoom.GetRoomStatus();
        this.radLstAvailable.DataSource = dt;
        this.radLstAvailable.DataTextField = Dic_MeetingRoom.SQLCOL_PROPERTYNAME;
        this.radLstAvailable.DataValueField = Dic_MeetingRoom.SQLCOL_PROPERTYID;
        this.radLstAvailable.DataBind();
        if (this.radLstAvailable.Items.Count > 0)
            this.radLstAvailable.Items[0].Selected = true;
    }
    #endregion

    #region 获取会议室信息
    private void LoadRoomInfo()
    {
        MeetingRooms info = new MeetingRooms(this.RoomID);
        info.Load();

        if (info.Picture == null || info.Picture.Equals(string.Empty))
            this.imgRoom.ImageUrl = "../images/NoPreview.jpg";
        else
            this.imgRoom.ImageUrl = "../images/MeetingRooms/" + info.Picture;

        this.imgRoom.Visible = true;
        this.ddlRoomType.SelectedValue = info.RoomType.ToString();
        this.txtRoomName.Text = info.RoomName;
        this.txtLocation.Text = info.Location;
        this.txtSeatsCount.Text = info.SeatsCount.ToString();
        this.txtDescript.Text = info.Description;
        this.radLstAvailable.SelectedValue = info.Flag;

        string Names = "", IDs = "";
        DataTable dt = BLMeetingRoomGroups.GetMeetingRoomGroups(this.RoomID);
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            IDs += ";" + dt.Rows[i][MeetingRoomGroups.SQLCOL_GROUPID].ToString();
            Names += ";" + dt.Rows[i][MeetingRoomGroups.SQLCOL_GROUPNAME].ToString();
        }
        if (IDs.Length > 0)
        {

        }
    }
    #endregion

    protected void btnSave_Click(object sender, System.EventArgs e)
    {
        int iSeatsCount;
        if (this.txtRoomName.Text == string.Empty)
        {
            WebCommon.ShowMessage(this.Page, Util.SceneName + "名称不能为空.");
            return;
        }
        if (this.txtLocation.Text == string.Empty)
        {
            WebCommon.ShowMessage(this.Page, Util.SceneName + "位置不能为空.");
            return;
        }
        try
        {
            iSeatsCount = int.Parse(this.txtSeatsCount.Text);
        }
        catch
        {
            iSeatsCount = 1;
        }

        MeetingRooms info = new MeetingRooms();
        if (this.RoomID == int.MinValue)
        {
            #region Upload Room Photo
            try
            {
                //检查上传文件不为空
                if (this.flRoomPic.PostedFile.FileName.Trim() != "")
                {
                    string fileName;
                    string extendName;
                    string originalPath = this.flRoomPic.PostedFile.FileName;
                    int i = originalPath.LastIndexOf(".");
                    int j = originalPath.LastIndexOf("\\");
                    extendName = originalPath.Substring(i);
                    if (!JudgeExtendName(extendName))
                    {
                        return;
                    }
                    fileName = Guid.NewGuid().ToString() + extendName;

                    if ((long)this.flRoomPic.PostedFile.InputStream.Length / 1024 < WebCommon.MaxUploadSize)
                        this.flRoomPic.PostedFile.SaveAs(Request.PhysicalApplicationPath + "\\images\\MeetingRooms\\" + fileName);
                    else
                    {
                        WebCommon.ShowMessage(this.Page, "上传文件过大，请调整后上传\n上传限制大小为：" + WebCommon.MaxUploadSize.ToString() + "KB");
                        return;
                    }
                    info.Picture = fileName;
                }
            }
            catch (Exception ex)
            {
                WebCommon.ShowMessage(this.Page, "上传图片失败." + ex.Message);
                return;
            }
            #endregion

            #region Insert Into MeetingRooms
            info.RoomType = int.Parse(this.ddlRoomType.SelectedValue);
            info.RoomName = this.txtRoomName.Text;
            info.Location = this.txtLocation.Text;
            info.SeatsCount = iSeatsCount;
            info.GroupID = BasePage.GetDept(CurrentUser.ID).ID;
            info.GroupName = BasePage.GetDept(CurrentUser.ID).DisplayName;
            info.Conservator = CurrentUser.Name;
            info.ConservatorName = CurrentUser.DisplayName;
            info.Description = this.txtDescript.Text;
            info.Flag = this.radLstAvailable.SelectedValue;
            info.Add();

            #endregion
        }
        else
        {
            info.ID = this.RoomID;
            info.Load();

            #region Upload Room Photo
            try
            {
                //检查上传文件不为空
                if (this.flRoomPic.PostedFile.FileName.Trim() != "")
                {

                    string fileName;
                    string extendName;
                    string originalPath = this.flRoomPic.PostedFile.FileName;
                    int i = originalPath.LastIndexOf(".");
                    int j = originalPath.LastIndexOf("\\");
                    extendName = originalPath.Substring(i);
                    if (!JudgeExtendName(extendName))
                    {
                        return;
                    }
                    fileName = Guid.NewGuid().ToString() + extendName;

                    if (System.IO.File.Exists(Request.PhysicalApplicationPath + "\\images\\MeetingRooms\\" + info.Picture))
                        System.IO.File.Delete(Request.PhysicalApplicationPath + "\\images\\MeetingRooms\\" + info.Picture);

                    try
                    {
                        if ((long)this.flRoomPic.PostedFile.InputStream.Length / 1024 < WebCommon.MaxUploadSize)
                        {
                            this.flRoomPic.PostedFile.SaveAs(Request.PhysicalApplicationPath + "\\images\\MeetingRooms\\" + fileName);
                        }
                        else
                        {
                            WebCommon.ShowMessage(this.Page, "上传文件过大，请调整后上传\n上传限制大小为：" + WebCommon.MaxUploadSize.ToString() + "KB");
                            return;
                        }
                    }
                    catch (Exception ex)
                    {
                        WebCommon.ShowMessage(this.Page, ex.Message);
                        return;
                    }
                    info.Picture = fileName;
                }

            }
            catch
            {
                WebCommon.ShowMessage(this.Page, "上传图片失败.");
                return;
            }
            #endregion


            #region Update MeetingRooms
            info.RoomType = int.Parse(this.ddlRoomType.SelectedValue);
            info.RoomName = this.txtRoomName.Text;
            info.Location = this.txtLocation.Text;
            info.SeatsCount = iSeatsCount;
            info.GroupID = BasePage.GetDept(CurrentUser.ID).ID;
            info.GroupName = BasePage.GetDept(CurrentUser.ID).DisplayName;
            info.Conservator = CurrentUser.Name;
            info.ConservatorName = CurrentUser.DisplayName;
            info.Description = this.txtDescript.Text;
            info.Flag = this.radLstAvailable.SelectedValue;
            info.UpdateByPrimaryKey();
            #endregion

            BLMeetingRoomDevice.DelMeetingRoomDevice(info.ID);
        }
        ClientScript.RegisterStartupScript(this.GetType(), "RefreshParent", "<script>alert('保存成功');parent.document.getElementById('ctl00$ListToolBarPlaceHolder$btnRefresh').click();</script>");
        //WebCommon.ShowMessage(this.Page, "操作成功.");
    }

    private bool JudgeExtendName(string extendName)
    {
        if (!extendName.ToLower().Equals(".jpg") && !extendName.ToLower().Equals(".bmp") && !extendName.ToLower().Equals(".gif"))
        {
            WebCommon.ShowMessage(this.Page, "文件格式有误，请上传jpg、bmp或者gif文件");
            return false;
        }
        else
            return true;
    }

    protected void btnDelete_Click(object sender, System.EventArgs e)
    {
        MeetingRooms info = new MeetingRooms(RoomID);
        info.Delete();
        ClientScript.RegisterStartupScript(this.GetType(), "RefreshParent", "<script>alert('删除成功');parent.document.getElementById('ctl00$ListToolBarPlaceHolder$btnRefresh').click();</script>");
    }

    protected void btnReset_Click(object sender, System.EventArgs e)
    {
        if (this.RoomID == int.MinValue)
        {
            this.imgRoom.Visible = false;
            this.ddlRoomType.SelectedIndex = 0;
            this.txtRoomName.Text = string.Empty;
            this.txtLocation.Text = string.Empty;
            this.txtSeatsCount.Text = string.Empty;
            this.txtDescript.Text = string.Empty;
            this.radLstAvailable.SelectedIndex = 0;
        }
        else
        {
            LoadRoomInfo();
        }
    }

}
